package front_chat_api

import (
	"blog_go/models"
	"blog_go/models/ctype"
	"blog_go/models/res"
	"blog_go/service/common"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/sirupsen/logrus"
	"time"
)

type ChatRecordRsp struct {
	CreatedAt time.Time     `json:"created_at"` // 创建时间
	NickName  string        `json:"nick_name"`
	Avatar    string        `json:"avatar"`
	Content   string        `json:"content"`  // 聊天的内容
	MsgType   ctype.MsgType `json:"msg_type"` // 聊天类型
}

func (ChatApi) ChatList(c *gin.Context) {
	page := models.DefaultPageInfo()
	if err := c.ShouldBindQuery(&page); err != nil {
		logrus.Error(err)
		panic(err)
	}
	isGroup := true
	list, count, err := common.ComList(&models.ChatModel{
		IsGroup: &isGroup,
	}, page)
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	var rspList []ChatRecordRsp
	for _, m := range list {
		var rsp ChatRecordRsp
		err := copier.Copy(&rsp, &m)
		if err != nil {
			logrus.Error(err)
			panic(err)
		}
		rspList = append(rspList, rsp)
	}
	res.OkWithList(rspList, count, c)
}
